<?php

session_start();
ob_start();
include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$zen_order_id = isset($_POST["zen_order_id"]) ? intval($_POST["zen_order_id"]) : 0;
$by_product_customer = isset($_POST["by_product_customer"]) ? intval($_POST["by_product_customer"]) : 0;
$by_order_status = isset($_POST["by_order_status"]) ? intval($_POST["by_order_status"]) : 0;
$by_province = isset($_POST["by_province"]) ? intval($_POST["by_province"]) : 0;
$by_payment = isset($_POST["by_payment"]) ? intval($_POST["by_payment"]) : 0;
$search_text = isset($_POST["search_text"]) ? addslashes(trim($_POST["search_text"])) : '';
$customer_order_id = isset($_POST["customer_order_id"]) ? intval($_POST["customer_order_id"]) : 0;
$start_time = isset($_POST["start_time"]) ? addslashes(trim($_POST["start_time"])) : '';
$end_time = isset($_POST["end_time"]) ? addslashes(trim($_POST["end_time"])) : '';
if ($start_time != "" && $end_time != "") {
    if (strtotime($end_time) <= strtotime($start_time)) {
        echo json_encode(array('type' => 1, 'msg' => "Bạn phải chọn thời gian kết thúc lớn hơn thời gian bắt đầu!"));
        die;
    }
}
$where = '';
$order_by = '';
if ($by_order_status > 0) {
    switch ($by_order_status) {
        case 1:
            //store
            $where.='order_status=0';
            if ($start_time != "") {
                $where.=' AND store_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND store_date<"' . $end_time . '"';
            }
            $order_by .= "store_date asc";
            break;
        case 2:
            //online
            $where.='order_status=1';
            if ($start_time != "") {
                $where.=' AND online_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND online_date<"' . $end_time . '"';
            }
            $order_by .= "online_date asc";
            break;
        case 3:
            //hàng có sẵn
            $where.='order_status=2';
            break;
        case 4:
            //đã nhận hàng
            $where.='order_status=3';
            if ($start_time != "") {
                $where.=' AND received_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND received_date<"' . $end_time . '"';
            }
            $order_by .= "received_date asc";
            break;
        case 5:
            //đang shipping
            $where.='order_status=4';
            break;
        case 6:
            //khách đã nhận
            $where.='order_status=5';
            if ($start_time != "") {
                $where.=' AND saled_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND saled_date<"' . $end_time . '"';
            }
            $order_by .= "saled_date asc";
            break;
        case 7:
            //hết hàng
            $where.='order_status=6';
            break;
        case 8:
            //ORDER
            $where.='order_status=7';
            if ($start_time != "") {
                $where.=' AND customer_order_date >="' . $start_time . '"';
            }
            if ($end_time != "") {
                $where.=' AND customer_order_date <"' . $end_time . '"';
            }
            $order_by .= "customer_order_date asc";
            break;
        case 9:
            //chưa chắc chắn
            $where.='order_status=8';
            break;
    }
} else {
    if ($start_time != "") {
        if ($where == '') {
            $where.='(store_date >="' . $start_time . '" OR online_date >="' . $start_time . '" OR received_date >="' . $start_time . '"
            OR saled_date >="' . $start_time . '" OR customer_order_date >="' . $start_time . '")';
        } else {
            $where.=' AND (store_date >="' . $start_time . '" OR online_date >="' . $start_time . '" OR received_date >="' . $start_time . '"
            OR saled_date >="' . $start_time . '" OR customer_order_date >="' . $start_time . '")';
        }
    }
    if ($end_time != "") {
        if ($where == '') {
            $where.='(store_date<"' . $end_time . '" OR online_date<"' . $end_time . '" OR received_date<"' . $end_time . '" 
                  OR saled_date<"' . $end_time . '" OR customer_order_date <"' . $end_time . '")';
        } else {
            $where.=' AND (store_date<"' . $end_time . '" OR online_date<"' . $end_time . '" OR received_date<"' . $end_time . '" 
                  OR saled_date<"' . $end_time . '" OR customer_order_date <"' . $end_time . '")';
        }
    }
}
if ($by_payment > 0) {
    switch ($by_payment) {
        case 1:
            //thanh toán hết
            if ($where == '') {
                $where.='total_current_money=0';
            } else {
                $where.=' AND total_current_money=0';
            }
            break;
        case 2:
            //đã cọc
            if ($where == '') {
                $where.='total_payment > 0';
            } else {
                $where.=' AND total_payment > 0';
            }
            break;
        case 3:
            //chưa thanh toán
            if ($where == '') {
                $where.='total_payment = 0';
            } else {
                $where.=' AND total_payment = 0';
            }
            break;
    }
}
if ($search_text !== "") {
    if ($by_product_customer == 1) {
        if ($where == '') {
            $where.="product_code LIKE '%" . $search_text . "%'";
        } else {
            $where.=" AND product_code LIKE '%" . $search_text . "%'";
        }
    } elseif ($by_product_customer == 2) {
        if ($where == '') {
            $where.="facebook_name LIKE '%" . $search_text . "%'";
        } else {
            $where.=" AND facebook_name LIKE '%" . $search_text . "%'";
        }
    }
}
if ($by_province > 0) {
    switch ($by_province) {
        case 1:
            //tỉnh
            if ($where == '') {
                $where.='province=1';
            } else {
                $where.=' AND province=1';
            }
            break;
        case 2:
            //Hà Nội
            if ($where == '') {
                $where.='province=0';
            } else {
                $where.=' AND province=0';
            }
            break;
    }
}
$zen_customer_order = $core->getZenCustomerOrderById($customer_order_id);
if ($core->checkZenOrderByID($zen_order_id) && $zen_customer_order) {
    $total_result = array();
    $customer_order_detail = $core->getInfoByOrderStatus($zen_order_id, $customer_order_id, $where, $total_result, 0);
    $html_total_result = '';
    if ($customer_order_detail) {
        $html_total_result = '<table class="table table-striped table-bordered bootstrap-datatable datatable">
                        <tr>
                                <th>Tổng số khách hàng</th>
                                <th>Tổng số sản phẩm</th>';
        if ($_SESSION["user_role"] == 1) {
            $html_total_result.='<th>Tổng AT<br/>(EU)</th>
                                 <th>Tổng PT<br/>(EU)</th>';
        }

        $html_total_result.='<th>Tổng tiền hàng<br/>(x1000 VNĐ)</th>
                                <th>Tổng cọc<br/>(x1000 VNĐ)</th>
                                <th>Tổng thừa<br/>(x1000 VNĐ)</th>
                                <th>Tổng thiếu<br/>(x1000 VNĐ)</th>
                               
                            </tr>
                            <tr>
                                <td>' . $total_result["total_customer"] . '</td>
                                <td>' . $total_result["total_product"] . '</td>';
        if ($_SESSION["user_role"] == 1) {
            $html_total_result.='<td>' . number_format($total_result["total_at"]) . '</td>
                                 <td>' . number_format($total_result["total_pt"]) . '</td>';
        }
        $html_total_result.='<td>' . number_format($total_result["total_order_money"] / 1000) . '</td>
                                <td>' . number_format($total_result["total_payment"] / 1000) . '</td>
                                <td>' . number_format($total_result["total_excess_cash"] / 1000) . '</td>
                                <td>' . number_format($total_result["total_debit"] / 1000) . '</td>
                                
                            </tr>
                    </table>';

        //render html for only customer order id
        $customer_order_id = 0;
        $var_count =isset($_POST["stt"]) ? intval($_POST["stt"]) : 0;
        $html='';
        foreach ($customer_order_detail as $customer_order) {
            $random_code = $customer_order->code_view;
            $color_photo_name = $customer_order->product_color . "_" . $customer_order->product_code . "_" . $customer_order->manufacture;
            $color_photo_name = $core->convert_string_friendly($color_photo_name) . ".jpg";
            $path_color_photo = dirname(dirname(__FILE__)) . DS . "photo" . DS . "extra_photo" . DS . "thumb_extra_photo" . DS . $color_photo_name;

            if (file_exists($path_color_photo)) {
                $src_photo = 'photo/extra_photo/' . $color_photo_name;
                $src_photo_thumb = 'photo/extra_photo/thumb_extra_photo/' . $color_photo_name;
            } else {
                $src_photo = 'photo/' . $customer_order->main_photo;
                $src_photo_thumb = 'photo/thumb_photo/200_' . $customer_order->main_photo;
            }
            if ($customer_order_id != $customer_order->customer_order_id) {
                
                $customer_order_id = $customer_order->customer_order_id;
                if ($customer_order->total_current_money > 0) {
                    $excess_cash = number_format($customer_order->total_current_money / 1000);
                    $debit = 0;
                } else {
                    $excess_cash = 0;
                    $debit = number_format($customer_order->total_current_money / 1000);
                }
                $html.='<tr class="tr_' . $customer_order->customer_order_id . '">';
                $html.='<td class="center" rowspan="' . $customer_order->total_product . '">' . $var_count . '</td>';
                $html.='<td class="center" rowspan="' . $customer_order->total_product . '">';
                if ($customer_order->facebook_link != "") {
                    $html.='<a href="' . $customer_order->facebook_link . '" target="_blank">' . $customer_order->facebook_name . '</a>';
                } else {
                    $html.=$customer_order->facebook_name;
                }
                $html.='<p></p>
                        <select class="select_customer_order_status" id="select_customer_order_status_' . $customer_order->customer_order_id . '" style="width: 120px;" rel="' . $customer_order->customer_order_id . '" >
                            <option value=0 ' . ($customer_order->customer_order_status == 0 ? "selected" : "") . '>Store</option>
                            <option value=1 ' . ($customer_order->customer_order_status == 1 ? "selected" : "") . '>Online</option>
                            <option value=2 ' . ($customer_order->customer_order_status == 2 ? "selected" : "") . '>Hàng có sẵn</option>
                            <option value=3 ' . ($customer_order->customer_order_status == 3 ? "selected" : "") . '>Đã nhận hàng</option>
                            <option value=4 ' . ($customer_order->customer_order_status == 4 ? "selected" : "") . '>Đang shipping</option>
                            <option value=5 ' . ($customer_order->customer_order_status == 5 ? "selected" : "") . '>Khách đã nhận</option>
                            <option value=6 ' . ($customer_order->customer_order_status == 6 ? "selected" : "") . '>Hết hàng</option>
                            <option value=7 ' . ($customer_order->customer_order_status == 7 ? "selected" : "") . '>ORDER</option>
                            <option value=8 ' . ($customer_order->customer_order_status == 8 ? "selected" : "") . '>Chưa chắc chắn</option>
                            <option value=9 ' . ($customer_order->customer_order_status == 9 ? "selected" : "") . '>Chưa xác định</option>
                        </select>';

                $html.=' <p></p><button  class="btn btn-primary print_address_customer" type="button" rel="'.$customer_order->customer_id.'" >Print address</button>
                 <p></p>
                <a target="_blank" href="../customerOrder.php?id='.$customer_order->customer_order_id.'&code='.$random_code.'">Link đơn hàng</a>';
                if($customer_order->type_calculate_payment !='normal'){
                    $html.= "<p></p><b>Khách buôn (".strtoupper($customer_order->type_calculate_payment).")</b>";
                }
                $html.='</td>';

                $html.='<td class="center" rowspan="' . $customer_order->total_product . '"><a href="addCustomer.php?id=' . $customer_order->customer_id . '" target="_blank">' . $customer_order->phone . '</a></td>';
                if ($customer_order->link_web == "") {
                    $html.='<td class="center color_status">' . $customer_order->product_code . '</td>';
                } else {
                    $html.='<td class="center color_status"><a href="' . $customer_order->link_web . '" target="_blank">' . $customer_order->product_code . '</a></td>';
                }


                $html.='<td class="center color_status">' . $customer_order->product_size . '</td>
                            <td class="center color_status">
                                Khách đặt: '.$customer_order->order_product_num.'
                                <p>
                                    Đã mua: <span id="show_item_product_num_'.$customer_order->id.'">'.$customer_order->product_num.'</span>
                                    <div class="input-append">

                                        <input style="width: 50px;display: none" type="text" id="mrk_bought_product_num_'.$customer_order->id.'" name="customer_phone" size="5" value="'.$customer_order->product_num.'" >
                                    <button class="mrk_edit_product_num btn"  type="button" data-item-id="'.$customer_order->id.'">Sửa</button>
                                    <button class="mrk_update_product_num btn" style="display: none" type="button" data-item-id="'.$customer_order->id.'">Lưu</button>
                                    </div>
                                </p>
                            </td>
                            <td class="center color_status">' . $customer_order->product_color . '</td>
                            <td class="center color_status"><img rel="' . $src_photo . '" class="photo_product" src="' . $src_photo_thumb . '" width="100px"></td>
                            <td class="center color_status">
                                <select class="select_order_status common_order_status_' . $customer_order->customer_order_id . '" style="width: 120px;" rel="' . $customer_order->id . '" >
                                    <option value=0 ' . ($customer_order->order_status == 0 ? "selected" : "") . '>Store</option>
                                    <option value=1 ' . ($customer_order->order_status == 1 ? "selected" : "") . '>Online</option>
                                    <option value=2 ' . ($customer_order->order_status == 2 ? "selected" : "") . '>Hàng có sẵn</option>
                                    <option value=3 ' . ($customer_order->order_status == 3 ? "selected" : "") . '>Đã nhận hàng</option>
                                    <option value=4 ' . ($customer_order->order_status == 4 ? "selected" : "") . '>Đang shipping</option>
                                    <option value=5 ' . ($customer_order->order_status == 5 ? "selected" : "") . '>Khách đã nhận</option>
                                    <option value=6 ' . ($customer_order->order_status == 6 ? "selected" : "") . '>Hết hàng</option>
                                    <option value=7 ' . ($customer_order->order_status == 7 ? "selected" : "") . '>ORDER</option>
                                    <option value=8 ' . ($customer_order->order_status == 8 ? "selected" : "") . '>Chưa chắc chắn</option>
                                </select>';
                if ($customer_order->order_status == 0) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->store_date) . '</div>';
                } elseif ($customer_order->order_status == 1) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->online_date) . '</div>';
                } elseif ($customer_order->order_status == 3) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->received_date) . '</div>';
                } elseif ($customer_order->order_status == 5) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->saled_date) . '</div>';
                } elseif ($customer_order->order_status == 7) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->customer_order_date) . '</div>';
                }
                $html.='<div class="history_status"></div>
                            </td>
                            <td class="center color_status">
                                <p class="description_product_detail" data-item-id="' . $customer_order->id . '">'.nl2br(htmlspecialchars($customer_order->description)).'</p>
                                <button class="popup_edit_note_product" class="btn" type="button" rel="' . $customer_order->id . '">Sửa</button>
                            </td>
                            <td class="center gp_3" id="td_price_' . $customer_order->id . '">' . number_format($customer_order->product_price / 1000) . '</td>
                            <td class="center gp_2" id="td_stamp_price_' . $customer_order->id . '">' . number_format($customer_order->stamp_price) . '</td>
                            <td class="center gp_1" id="td_store_price_' . $customer_order->id . '">' . number_format($customer_order->store_price) . '</td>
                            <td class="center gp_2" id="td_total_stamp_price_' . $customer_order->customer_order_id . '" rowspan="' . $customer_order->total_product . '">' . number_format($customer_order->total_at) . '</td>
                            <td class="center gp_1" id="td_total_store_price_' . $customer_order->customer_order_id . '" rowspan="' . $customer_order->total_product . '">' . number_format($customer_order->total_pt) . '</td>
                            <td class="center gp_3" id="td_total_price_' . $customer_order->customer_order_id . '" rowspan="' . $customer_order->total_product . '">' . number_format($customer_order->total_order_money / 1000) . '</td>
                            <td class="center gp_3"  rowspan="' . $customer_order->total_product . '">
                                <a  class="popup_zen_payment" id="href_payment_' . $customer_order->customer_order_id . '" href="javascript:void(0);" rel="' . $customer_order->customer_order_id . '">' . number_format($customer_order->total_payment / 1000) . '</a>
                            </td>
                             <td class="center gp_3" id="td_debit_' . $customer_order->customer_order_id . '" rowspan="' . $customer_order->total_product . '">' . $debit . '</td>
                            <td class="center gp_3" id="td_excess_cash_' . $customer_order->customer_order_id . '" rowspan="' . $customer_order->total_product . '">' . $excess_cash . '</td>
                             <td class="center" rowspan="' . $customer_order->total_product . '">' . $customer_order->order_description . '</td>
                             <td class="center gp_4" rowspan="' . $customer_order->total_product . '">' . $customer_order->address . '</td>
                            <td class="center gp_4" rowspan="' . $customer_order->total_product . '">';

                if ($customer_order->province == 1)
                    $html.= "Ở tỉnh";
                else
                    $html.= "Ở HN";

                $html.='</td>
                            <td class="center gp_4" rowspan="' . $customer_order->total_product . '">' . $customer_order->name . '</td>
                            <td class="center gp_4" rowspan="' . $customer_order->total_product . '">' . $customer_order->bank_account . '</td>
                            <td class="center" rowspan="' . $customer_order->total_product . '">
                                <a class="btn btn-info btn_edit_customer_order" href="javascript:void(0)" rel="' . $customer_order->customer_order_id . '">
                                    <i class="icon-edit icon-white"></i>  
                                    Edit                                            
                                </a>
                                <a rel="' . $customer_order->customer_order_id . '" class="btn btn-danger btn_delete_order_detail" href="javascript:void(0)" style=" margin: 10px 0px;">
                                    <i class="icon-trash icon-white"></i> 
                                    Delete
                                </a>
                            </td>
                        </tr>';
            } else {

                $html.=' <tr class="tr_' . $customer_order->customer_order_id . '">';
                if ($customer_order->link_web == "") {
                    $html.='<td class="center color_status">' . $customer_order->product_code . '</td>';
                } else {
                    $html.='<td class="center color_status"><a href="' . $customer_order->link_web . '" target="_blank">' . $customer_order->product_code . '</a></td>';
                }


                $html.='<td class="center color_status">' . $customer_order->product_size . '</td>
                            <td class="center color_status">
                                Khách đặt: '.$customer_order->order_product_num.'
                                <p>
                                    Đã mua: <span id="show_item_product_num_'.$customer_order->id.'">'.$customer_order->product_num.'</span>
                                    <div class="input-append">

                                        <input style="width: 50px;display: none" type="text" id="mrk_bought_product_num_'.$customer_order->id.'" name="customer_phone" size="5" value="'.$customer_order->product_num.'" >
                                    <button class="mrk_edit_product_num btn"  type="button" data-item-id="'.$customer_order->id.'">Sửa</button>
                                    <button class="mrk_update_product_num btn" style="display: none" type="button" data-item-id="'.$customer_order->id.'">Lưu</button>
                                    </div>
                                </p>
                            </td>
                            <td class="center color_status">' . $customer_order->product_color . '</td>
                            <td class="center color_status"><img rel="' . $src_photo . '" class="photo_product" src="' . $src_photo_thumb . '" width="100px"></td>
                            <td class="center color_status">
                                <select class="select_order_status common_order_status_' . $customer_order->customer_order_id . '" style="width: 120px;" rel="' . $customer_order->id . '">
                                    <option value=0 ' . ($customer_order->order_status == 0 ? "selected" : "") . '>Store</option>
                                    <option value=1 ' . ($customer_order->order_status == 1 ? "selected" : "") . '>Online</option>
                                    <option value=2 ' . ($customer_order->order_status == 2 ? "selected" : "") . '>Hàng có sẵn</option>
                                    <option value=3 ' . ($customer_order->order_status == 3 ? "selected" : "") . '>Đã nhận hàng</option>
                                    <option value=4 ' . ($customer_order->order_status == 4 ? "selected" : "") . '>Đang shipping</option>
                                    <option value=5 ' . ($customer_order->order_status == 5 ? "selected" : "") . '>Khách đã nhận</option>
                                    <option value=6 ' . ($customer_order->order_status == 6 ? "selected" : "") . '>Hết hàng</option>
                                    <option value=7 ' . ($customer_order->order_status == 7 ? "selected" : "") . '>ORDER</option>
                                    <option value=8 ' . ($customer_order->order_status == 8 ? "selected" : "") . '>Chưa chắc chắn</option>
                                </select>';
                if ($customer_order->order_status == 0) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->store_date) . '</div>';
                } elseif ($customer_order->order_status == 1) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->online_date) . '</div>';
                } elseif ($customer_order->order_status == 3) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->received_date) . '</div>';
                } elseif ($customer_order->order_status == 5) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->saled_date) . '</div>';
                } elseif ($customer_order->order_status == 7) {
                    $html.='<div>' . $core->changeDateMysqlToShow($customer_order->customer_order_date) . '</div>';
                }
                $html.='<div class="history_status"></div>
                            </td>
                            <td class="center color_status">
                                <p class="description_product_detail" data-item-id="' . $customer_order->id . '">'.nl2br(htmlspecialchars($customer_order->description)).'</p>
                                <button class="popup_edit_note_product" class="btn" type="button" rel="' . $customer_order->id . '">Sửa</button>
                            </td>
                            <td class="center gp_3" id="td_price_' . $customer_order->id . '">' . number_format($customer_order->product_price / 1000) . '</td>
                             <td class="center gp_2" id="td_stamp_price_' . $customer_order->id . '">' . number_format($customer_order->stamp_price) . '</td>
                            <td class="center gp_1" id="td_store_price_' . $customer_order->id . '">' . number_format($customer_order->store_price) . '</td>';
                $html.='</tr>';
            }
        }

        echo json_encode(array('type' => 0, 'html_total_result' => $html_total_result,'html'=>$html
        ));
        die;
    } else {
        echo json_encode(array('type' => 1, 'msg' => "Có lỗi xảy ra."));
        die;
    }
} else {
    echo json_encode(array('type' => 1, 'msg' => "Có lỗi xảy ra, vui lòng f5 lại trang."));
    die;
}
?>